/**
 * @author		Ben Layet
 * @version 	1.0
 * @date		25 Sep 2008
 */
package org.ignoramus.application.store;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * The I-gnoramus file.
 *
 */
public class LessonFile implements LessonStore
{
	/** The I-gnoramus file. */
	private final ZipFile file;

	/** The XML lesson file name. */
	private static final String xml_lesson_filename = "lesson.xml";
	/** The lesson file. */
	private XmlLessonParser xmlLessonFile = null;
	

	/**
	 * Constructs from a file name.
	 * @param filename the name of the file. 
	 * @throws StoreException if the filename is not valid.
	 */
	public LessonFile(String filename) throws StoreException
	{
		try
		{
			file = new ZipFile(filename);
		}
		catch (IOException e)
		{
			throw new StoreException("Invalid file: " + filename, e);
		}
	}
	
	/**
	 * Initialise the lesson file.
	 * 
	 * @throws StoreException if the file does not contain the XMl lesson file.
	 */
	private void initialise() throws StoreException
	{
		//check if the initialisation has already been done
		if(xmlLessonFile == null)
		{
			//get the zip entry
			ZipEntry entry = file.getEntry(xml_lesson_filename);
			try
			{
				//get the input stream
				xmlLessonFile = new XmlLessonParser(file.getInputStream(entry));
			}
			catch (IOException e)
			{
				throw new StoreException("The zip file does not contain " + xml_lesson_filename, e);
			}
		}
	}

	/**
	 * Gets the input stream of a file inside the zip file
	 * @param filename the name of the file desired.
	 * @return the input stream of the file.
	 * @throws StoreException if the file does not contain the desired file.
	 */
	private InputStream getData(String filename) throws StoreException
	{
		//get the zip entry
		ZipEntry entry = file.getEntry(filename);
		try
		{
			//get the input stream
			return file.getInputStream(entry);
		}
		catch (IOException e)
		{
			throw new StoreException("The zip file does not contain " + xml_lesson_filename, e);
		}
	}

	/**
	 * Gets the input stream from the stored cards.
	 * @return the cards data.
	 * @throws StoreException if the file does not contain the desired file.
	 */
	public InputStream getCardsData() throws StoreException
	{
		//initialise
		initialise();
		//return the input stream
		return getData(xmlLessonFile.getCardsFilename());
	}
	
	/**
	 * Gets the input stream from the stored card template.
	 * @return the log data.
	 * @throws StoreException if the file does not contain the desired file.
	 */
	public InputStream getTemplateData() throws StoreException
	{
		//initialise
		initialise();
		//return the input stream
		return getData(xmlLessonFile.getTemplateFilename());
	}

	/**
	 * Gets the input stream from the stored log.
	 * @return the template data.
	 * @throws StoreException if the file does not contain the desired file.
	 */
	public InputStream getLogData() throws StoreException
	{
		//initialise
		initialise();
		//return the input stream
		return getData(xmlLessonFile.getLogFilename());
	}
	

	/**
	 * @returns The title of the lesson.
	 * @throws StoreException
	 *             if the file cannot be parsed.
	 */
	public String getTitle() throws StoreException
	{
		//initialise
		initialise();
		//get the title
		return xmlLessonFile.getTitle();
	}
}
